package k2_oop.p6_relation.demo08;

/**
 * 班级与老师学生是一种长期稳定的关系，所以属于关联关系
 */
public class Classes {
    //需要把学生和教师作为属性放在班级类中
    private Teacher teacher;
    private Student[] students = new Student[5];
    private int studentCount = 0;

    public Classes(Teacher teacher, Student[] students) {
        this.teacher = teacher;
        this.studentCount = students.length;
        for (int i = 0; i < students.length; i++) {
            this.students[i] = students[i];
        }
    }

    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }

    /**
     * 向数组中添加新学生，只做追加，不做插入
     * @param student
     */
    public void addStudent(Student student){
        if(studentCount < students.length){
            students[studentCount] = student;
            studentCount++;
            System.out.println("添加成功");
        }else {
            System.out.println("班级已经满员了");
        }
    }

    /**
     * 从数组中移除某个位置的学生
     * @param index
     */
    public void removeStudent(int index){
        for (int i = index; i < students.length-1 ; i++) {
            students[i] = students[i+1];
        }
        students[students.length-1] = null;
        studentCount--;
    }


    public void display(){
        System.out.println(this.teacher.getSubject()+"==="+this.teacher.getLevel());
        for (int i = 0; i < studentCount; i++) {
            System.out.println(this.students[i].getName()+"---"+this.students[i].getAge());
        }
        System.out.println("现有学生"+this.studentCount+"个");

        System.out.println("===================================================");
    }
}
